package com.kotlin.android.platoon.app

import com.android.app.router.path.RouterActivityPath
import com.kotlin.android.app.BuildConfig
import com.kotlin.android.common.album.AlbumManager
import com.kotlin.android.common.album.config.AlbumConfig
import com.kotlin.android.core.CoreApp
import com.kotlin.android.core.exception.CustomUnCaughtExceptionHandler
import com.kotlin.android.ktx.ext.log.e
import com.kotlin.android.ktx.ext.toast.showSystemToast
import com.kotlin.android.media.image.coil.loadImage
import com.kotlin.android.qb.JsManager
import com.kotlin.android.qb.ext.WebViewUtils
import com.kotlin.android.retrofit.RetrofitManager
import com.kotlin.android.router.RouterManager
import com.kotlin.android.widget.refresh.Refresh
import kotlin.properties.Delegates

/**
 */
class App : CoreApp() {

    companion object {
        var instance: App by Delegates.notNull()
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        RouterManager.instance.initialize(
            this,
            BuildConfig.DEBUG,
            clazz = RouterActivityPath::class.java
        )
        RetrofitManager.initialize(this, BuildConfig.DEBUG)
        CustomUnCaughtExceptionHandler.openState(this, BuildConfig.DEBUG.not())
        Refresh.initHeaderAndFooter()
        initAlbum()
//        isPrivacy = getSpValue(KEY_PRIVACY, false)
//        "isPrivacy = ${isPrivacy}".e()
        if (isPrivacy) {
            initSdk()
        }
    }

    private fun initAlbum() {
        AlbumManager.init(
            applicationContext = this,
            config = AlbumConfig(
                loadImage = { imageView, data, defaultImgRes, onSuccess ->
                    imageView?.loadImage(
                        data = data,
                        defaultImgRes = defaultImgRes,
                        onSuccess = onSuccess
                    )
                },
                showToast = {
                    showSystemToast(it)
                },
                upload = { photos, progressBar, error, success ->
                    // needUpload = true 时，启用通用的图片上传api
                    "尚未实现通用的图片上传API".e()
                }
            ),
        )
    }

    fun initSdk() {
//        LocationManager.instance.initialized(this)
        JsManager.initQbSdk(this)
    }


    fun setWebViewProcess() {
        WebViewUtils.setWebViewDataDirectory(this)
    }
}
